package com.anlogic.sdk.ui.tests.uc2.projectcreate.bspapp;

import static org.junit.Assert.*;

import java.util.regex.Pattern;

import javax.swing.plaf.synth.SynthOptionPaneUI;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.waits.ICondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.anlogic.sdk.ui.tests.utils.BspAppTestUtils;
import com.anlogic.sdk.ui.tests.utils.WorkspaceUtils;

public class CreateCHERRYUSB_MSC_RTTHREADTest {
	private static SWTWorkbenchBot bot;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		bot = new SWTWorkbenchBot();
		
		bot.sleep(2000);
		SWTBotPreferences.TIMEOUT = 10000;
		SWTBotPreferences.PLAYBACK_DELAY = 500;
	}

	

	@Before
	public void setUp() throws Exception {
		WorkspaceUtils.resetworkbench(bot);
	}

	@After
	public void tearDown() throws Exception {
		WorkspaceUtils.cleanWorkspace();
	}

	@Test
	public void rtthreadDr1m90core064Test() throws Exception{
		BspAppTestUtils.rtthreadDr1m90core064TestAndLib(bot, "CHERRYUSB_HOST_MSC_RTTHREAD");
	}
	
	
	
	@Test
	public void rtthreadDr1m90core164Test() throws Exception{
		BspAppTestUtils.rtthreadDr1m90core164TestAndLib(bot, "CHERRYUSB_HOST_MSC_RTTHREAD");

	}
	
	@Test
	public void rtthreadDr1v90core064Test() throws Exception{
		BspAppTestUtils.rtthreadDr1v90core064TestAndLib(bot, "CHERRYUSB_HOST_MSC_RTTHREAD");

	}
	protected ICondition consoleTextMatches(SWTBotView consoleView, Pattern pattern) {
		return new ConsoleTextMatches(consoleView, pattern);
	}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
	public static void cleanWorkspace() throws CoreException {
	    IWorkspace workspace = ResourcesPlugin.getWorkspace();
	    for (IProject project : workspace.getRoot().getProjects()) {
	        project.delete(true, true, null);
	    }
	}
	class ConsoleTextMatches implements ICondition {
		private final SWTBotView view;
		private Pattern pattern;

		public ConsoleTextMatches(SWTBotView view, Pattern pattern) {
			this.view = view;
			this.pattern = pattern;
		}

		@Override
		public boolean test() {
			if (view.isActive()) {
				String output = view.bot().styledText().getText();
				java.util.regex.Matcher m = pattern.matcher(output);
				return m.matches();
			}
			return false;
		}

		@Override
		public void init(SWTBot bot) {
		}

		@Override
		public String getFailureMessage() {
			return null;
		}
	}

}
